/**
 * 
 */
package com.google.code.jholidays.plugins.easter;

import java.util.Calendar;
import java.util.Date;

import com.google.code.jholidays.events.AbstractEvent;

/**
 * @author tillias
 * 
 */
public class Easter extends AbstractEvent {

    protected Easter(EasterProperties properties) {
	super(properties);
    }

    /**
     * {@inheritDoc}
     * <p>
     * Current implementation returns orthodoxly easter date
     */
    @Override
    public Date getDate(int year) {
	Date result = null;

	try {
	    int a = (19 * (year % 19) + 15) % 30;
	    int b = (2 * (year % 4) + 4 * (year % 7) + 6 * a + 6) % 7;

	    int oldStyleDay = -1;
	    boolean useMarch = false;

	    if (a + b > 9) {
		oldStyleDay = a + b - 9; // April
	    } else {
		oldStyleDay = 22 + a + b; // March
		useMarch = true;
	    }

	    Calendar calendar = Calendar.getInstance();

	    if (useMarch)
		calendar.set(year, Calendar.MARCH, oldStyleDay);
	    else
		calendar.set(year, Calendar.APRIL, oldStyleDay);

	    // convert to new style

	    calendar.add(Calendar.DAY_OF_MONTH, 13);

	    return calendar.getTime();
	} catch (Exception e) {
	    // nothing todo here
	}

	return result;
    }
}
